package so.sao.code.service.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import so.sao.code.dto.request.ThirdPartyCodeFileHandleRequest;
import so.sao.code.dto.response.ThirdPartyCodeFileHandleDto;
import so.sao.code.service.domain.entity.ThirdPartyCodeFileRecord;
import so.sao.code.service.infrastructure.enums.CodeMessage;
import so.sao.code.service.repository.ThirdPartyCodeFileRecordDao;
import so.sao.code.service.service.ThirdPartyCodeFileRecordService;
import so.sao.top.core.util.rest.RestException;

@Service
public class ThirdPartyCodeFileRecordServiceImpl implements ThirdPartyCodeFileRecordService {

	private static Logger logger = LoggerFactory.getLogger(ThirdPartyCodeFileRecordServiceImpl.class);

	@Autowired
	private ThirdPartyCodeFileRecordDao thirdPartyCodeFileRecordDao;

	@Override
	public ThirdPartyCodeFileHandleDto findByEseIdAndFileName(Long companyId, String fileName) {
		ThirdPartyCodeFileHandleDto dto = new ThirdPartyCodeFileHandleDto();
		ThirdPartyCodeFileRecord handle = thirdPartyCodeFileRecordDao.findByCompanyIdAndFileName(companyId, fileName);
		if (null != handle) {
			BeanUtils.copyProperties(handle, dto);
		}
		return dto;
	}

	@Override
	public ThirdPartyCodeFileHandleDto modify(ThirdPartyCodeFileHandleRequest codeFileHandleRequest) {
		// ThirdPartyCodeFileHandleDto response = new ThirdPartyCodeFileHandleDto();
		// ThirdPartyCodeFileRecord handle =
		// thirdPartyCodeFileRecordDao.findOne(codeFileHandleRequest.getId());
		// if (null == handle) {
		// throw new RestException(HttpStatus.NOT_ACCEPTABLE,
		// CodeMessage.InvalidRequest, codeFileHandleRequest);
		// }
		// 验证操作待确认
		// BeanUtils.copyProperties(codeFileHandleRequest, handle);
		// handle = thirdPartyCodeFileRecordDao.save(handle);
		// BeanUtils.copyProperties(handle, handle);
		// return response;
		return null;
	}

	@Override
	public String findTopCode(Long thirdPartyCode, String domain, String subDomain, Long eseId) {
		return "等待nosql端feign接口";
	}

	@Override
	public List<ThirdPartyCodeFileHandleDto> find(Long eseId, Date createDate) {
		List<ThirdPartyCodeFileRecord> handleList = thirdPartyCodeFileRecordDao.findByCompanyIdAndCreateDateAfter(eseId,
				createDate);
		if (null == handleList || handleList.isEmpty()) {
			throw new RestException(HttpStatus.NOT_ACCEPTABLE, CodeMessage.InvalidRequest, eseId, createDate);
		}
		ThirdPartyCodeFileHandleDto thirdPartyCodeFileHandleDto = null;
		List<ThirdPartyCodeFileHandleDto> tDtos = new java.util.Vector<>();
		for (ThirdPartyCodeFileRecord thirdPartyCodeFileHandle : handleList) {
			thirdPartyCodeFileHandleDto = new ThirdPartyCodeFileHandleDto();
			// 验证操作待确认
			BeanUtils.copyProperties(thirdPartyCodeFileHandleDto, thirdPartyCodeFileHandle);
			tDtos.add(thirdPartyCodeFileHandleDto);
		}
		return tDtos;
	}

}
